<template>
  <div class="app-container">
    <el-button type="primary" @click="saveGuideData()">发布</el-button>

    <el-form :model="article" :rules="rules" ref="dataForm" label-width="100px">
      <el-form-item label="内容" prop="content">
        <mavon-editor v-model="article.content" ref=md @imgAdd="handleEditorImgAdd"></mavon-editor>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {saveArticle, uploadFile} from "@/api/article";

export default {
  name: "articleCreate",
  data() {
    return {
      article: {
        content: ''
      },
      rules: {
        content: [
          {required: true, message: '请输入内容', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    handleEditorImgAdd(pos, file) {
      var formdata = new FormData();
      formdata.append("file", file);
      uploadFile(formdata).then((response) => {
        console.log(response.data)
        var url = response.data.storeUrl;
        this.$refs.md.$img2Url(pos, "https://www.qiangesoft.cn:8888/" +url);
      });
    },
    saveGuideData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const htmlContent = this.converter.makeHtml(this.article.content);
          var data = {content: this.article.content, html: htmlContent};
          console.log(data);
          saveArticle(data).then(() => {
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
